<template> 
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="roleTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="标题" align="center">
          <template slot-scope="scope">{{scope.row.title}}</template>
        </el-table-column>  
        <el-table-column label="添加时间" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="类型" align="center">
          <template slot-scope="scope">
            {{scope.row.type ? '图片' : '文字'}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope"> 
            <el-row>
            <el-button size="mini"  type="text" @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini" type="text"  @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="dialogVisible"
      width="1000px">
      <el-form :model="formData"  ref="roleForm" label-width="150px" size="small">
        <el-form-item label="标题：">
          <el-input v-model="formData.title" style="width: 300px"></el-input>
        </el-form-item>  
        <el-form-item label="类型：">
          <el-select v-model="formData.type" placeholder="请选择" style="width: 150px">
            <el-option label="文字" :value="0"></el-option>
            <el-option label="图片" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容：" v-if="formData.type==0">
          <tinymce :width="595" :height="300" v-model="formData.content"></tinymce>
        </el-form-item>
        <el-form-item label="图片：" v-if="formData.type==1">
          <multi-upload  v-model="formData.content"></multi-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {list,add,edit,del,detail} from '@/api/content'; 
  import {formatDate} from '@/utils/date';
  import Tinymce from '@/components/Tinymce'
  import MultiUpload from '@/components/Upload/multiUpload'

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    keyword: null
  };
  const defaultData = {
    title: '',
    content: '',
    type: 0,
  };
  export default {
    name: 'articleList',
    components: {Tinymce,MultiUpload},
    data() {
      return {
        editId:'',
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        formData: Object.assign({}, defaultData),
        isEdit: false,
        categoryList:[]
      }
    },
    created() {
      this.getList(); 
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {   
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.formData = Object.assign({},defaultData);
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该角色?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let id = [];
          id.push(row.id);
          let params=new URLSearchParams();
          params.append("id",id);
          del(params).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      async handleUpdate(index, row) {
      await detail({id:row.id}).then(data=>{
          for (const key in this.formData) {
            if (data.data[key] != null && data.data[key] != undefined) { 
              this.formData[key] = data.data[key]
            }
          }  
          if(this.formData.type==1){
            this.formData.content = data.data.content.split(',');
          }
          console.log('vvv',this.formData)
          this.dialogVisible = true;
          this.editId = row.id;
          this.isEdit = true; 
      })
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            const result = JSON.parse(JSON.stringify(this.formData));
            result.id = this.editId;
            if(result.type==1){
              result.content = this.formData.content.join(',');
            } 
            edit(result).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.formData = Object.assign({},defaultData);
              this.formData.content = '';
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            const result = JSON.parse(JSON.stringify(this.formData));
            if(result.type==1){
              result.content = this.formData.content.join(',');
            } 
            add(result).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.formData = Object.assign({},defaultData);
              this.formData.content = '';
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },  
      getList() {
        this.listLoading = true;
        list(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  }
</script>
<style></style>


